package j2024.j202407;

import java.util.Scanner;
class TreeNode {
     int val;
     TreeNode left;
     TreeNode right;
     TreeNode() {}
     TreeNode(int val) { this.val = val; }
     TreeNode(int val, TreeNode left, TreeNode right) {
         this.val = val;
         this.left = left;
         this.right = right;
     }
 }
public class j0725 {
    public int getLongestPalindrome (String A) {
        // write code here
        if(A.length()==1){
            return 1;
        }
        int max = -1;
        int n = A.length();
        char[] s = A.toCharArray();
        for (int i = 0; i < n; i++) {
            int ou = getlen(s,i,i);
            int ji = getlen(s,i,i+1);
            int tmp  = Math.max(ou,ji);
            max = Math.max(max,tmp);
        }
        return max;
    }
    public int getlen(char[] s,int l,int  r){
        while(l>=0 && r<s.length && s[l]==s[r]){
            l--;
            r++;
        }
        return r-l+1;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] val = new int[n];
        for (int i = 0; i < n; i++) {
            val[i] = in.nextInt();
        }
        int[] dp = new int[n+1];
        int min = val[0];
        for (int i = 1; i < n; i++) {
            dp[i] = Math.max(dp[i-1],val[i]-min);
            min = Math.min(min,val[i]);
        }
        System.out.println(dp[n-1]);
    }


    /**
     * 2331. 计算布尔二叉树的值
     * @param root
     * @return
     */
    public boolean evaluateTree(TreeNode root) {
        if(root.left==null){
            return root.val == 0 ? false : true;
        }
        boolean left = evaluateTree(root.left);
        boolean right = evaluateTree(root.right);
        return root.val == 2 ? left||right : left && right;
    }

    /**
     * 129. 求根节点到叶节点数字之和
     * @param root
     * @return
     */
    public int sumNumbers(TreeNode root) {
        return sum(root,0);
    }
    public int sum(TreeNode root,int presum){
        presum = presum*10+root.val;
        if(root.left==null && root.right==null){
            return presum;
        }
        int ret = 0;
        if(root.left!=null){
            ret+= sum(root.left,presum);
        }
        if(root.right!=null){
            ret+= sum(root.right,presum);

        }
        return ret;
    }

    /**
     * 1004. 最大连续1的个数 III
     * @param nums
     * @param k
     * @return
     */
    public int longestOnes(int[] nums, int k) {
        int n = nums.length;
        int left = 0,right = 0;
        int zero = 0;
        int ret = 0;
        while(right<n){
            if(nums[right]==0){
                zero++;
            }
            while(zero>k){
                if(nums[left]==0){
                    zero--;
                }
                left++;
            }
            ret = Math.max(ret,right-left+1);
            right++;

        }
        return ret;
    }
}
